package com.cy.dda.web.front.cases;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.enums.cases.CasesType;
import com.cy.dda.model.query.cases.CasesListQuery;
import com.cy.dda.model.vo.cases.CasesDetailInfoVO;
import com.cy.dda.model.vo.cases.CasesListVO;
import com.cy.dda.model.vo.cases.CasesLoanDetailInfoVO;
import com.cy.dda.service.cases.CasesService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 前台-委案
 *
 * @author: tanyilin
 * @create: 2018年11月2日 下午5:50:44
 */
@Slf4j
@RestController
@RequestMapping("/user/cases")
@Api(tags = "前台-委案")
public class CasesController extends BaseController {

	@Autowired
	private CasesService casesService;
	
	@ApiOperation("用户可选择的委案列表")
	@GetMapping("/selectable/list")
	public PageBean<CasesListVO> selectableList(CasesListQuery query) {
		Long userId = this.getUser().getUserId();
		query.setFlag(1);
		query.setUserId(userId);
		PageBean<CasesListVO> pageBean = casesService.selectCasesList(query);
		return pageBean;
	}
	
	@ApiOperation("我的委案列表")
	@GetMapping("/list")
	public PageBean<CasesListVO> myList(CasesListQuery query) {
		state(!CommonUtils.isEmpty(query.getFlag()), "委案列表类型不可为空");
		Long userId = this.getUser().getUserId();
		query.setUserId(userId);
		PageBean<CasesListVO> pageBean = casesService.selectCasesList(query);
		return pageBean;
	}
	
	@ApiOperation("我的客户的机构的委案列表")
	@GetMapping("/my/customer/org/cases/list")
	public PageBean<CasesListVO> myCustomerOrgCasesList(CasesListQuery query) {
		PageBean<CasesListVO> pageBean = casesService.selectCasesList(query);
		return pageBean;
	}
	
	@ApiOperation("我的客户的月供委案列表")
	@GetMapping("/my/customer/monthly/cases/list")
	public PageBean<CasesListVO> myCustomerMonthlyCasesList(CasesListQuery query) {
		query.setCasesType(CasesType.MONTHLY_SUPPLY);
		PageBean<CasesListVO> pageBean = casesService.selectCasesList(query);
		return pageBean;
	}
	
	@ApiOperation("委案详情")
	@GetMapping("/detail")
	public Result<CasesDetailInfoVO> detail(@RequestParam Long casesId) {
		CasesDetailInfoVO casesDetailInfoVO = casesService.selectCasesDetailInfoVO(casesId);
		return ok("", casesDetailInfoVO);
	}
	
	@ApiOperation("委案-借款详情")
	@GetMapping("/loan/detail")
	public Result<CasesLoanDetailInfoVO> loanDetail(@RequestParam Long casesId) {
		CasesLoanDetailInfoVO casesLoanDetailInfoVO = casesService.selectCasesLoanDetailInfoVO(casesId);
		return ok("", casesLoanDetailInfoVO);
	}
	
	
	@ApiOperation("用户接受委案")
	@PostMapping("/do/accept")
	public Result doAcceptCases(@RequestParam(required = true) Long casesId) {
		casesService.acceptCases(casesId, this.getUser().getUserId());
		return ok();
	}
	
}
